/*
 *  Copyright (C) 2010 Zubanov Dmitry
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


/**
 *
 * @author Zubanov Dmitry aka javadimon, zubanov@gmail.com
 * Omsk, Russia, created 29.05.2010
 * (C) Copyright by Zubanov Dmitry
 */

package org.jssdb.core.josql;

public class CompareResultNumber {

    public int getResult(Object val1, Object val2, boolean isASC) {
        int result = 0;
        Number n1 = (Number) val1;
        Number n2 = (Number) val2;

        if (val1 instanceof Byte) {
            if (!isASC) {
                if (n1.byteValue() > n2.byteValue()) {
                    result = -1;
                }

                if (n1.byteValue() < n2.byteValue()) {
                    result = 1;
                }

                if (n1.byteValue() == n2.byteValue()) {
                    result = 0;
                }

            } else {
                if (n1.byteValue() < n2.byteValue()) {
                    result = -1;
                }

                if (n1.byteValue() > n2.byteValue()) {
                    result = 1;
                }

                if (n1.byteValue() == n2.byteValue()) {
                    result = 0;
                }
            }
        }

        if (val1 instanceof Short) {
            if (!isASC) {
                if (n1.shortValue() > n2.shortValue()) {
                    result = -1;
                }

                if (n1.shortValue() < n2.shortValue()) {
                    result = 1;
                }

                if (n1.shortValue() == n2.shortValue()) {
                    result = 0;
                }

            } else {
                if (n1.shortValue() < n2.shortValue()) {
                    result = -1;
                }

                if (n1.shortValue() > n2.shortValue()) {
                    result = 1;
                }

                if (n1.shortValue() == n2.shortValue()) {
                    result = 0;
                }
            }
        }

        if (val1 instanceof Integer) {
            if (!isASC) {
                if (n1.intValue() > n2.intValue()) {
                    result = -1;
                }

                if (n1.intValue() < n2.intValue()) {
                    result = 1;
                }

                if (n1.intValue() == n2.intValue()) {
                    result = 0;
                }

            } else {
                if (n1.intValue() < n2.intValue()) {
                    result = -1;
                }

                if (n1.intValue() > n2.intValue()) {
                    result = 1;
                }

                if (n1.intValue() == n2.intValue()) {
                    result = 0;
                }
            }
        }

        if (val1 instanceof Long) {
            if (!isASC) {
                if (n1.longValue() > n2.longValue()) {
                    result = -1;
                }

                if (n1.longValue() < n2.longValue()) {
                    result = 1;
                }

                if (n1.longValue() == n2.longValue()) {
                    result = 0;
                }

            } else {
                if (n1.longValue() < n2.longValue()) {
                    result = -1;
                }

                if (n1.longValue() > n2.longValue()) {
                    result = 1;
                }

                if (n1.longValue() == n2.longValue()) {
                    result = 0;
                }
            }
        }

        if (val1 instanceof Double) {
            if (!isASC) {
                if (n1.doubleValue() > n2.doubleValue()) {
                    result = -1;
                }

                if (n1.doubleValue() < n2.doubleValue()) {
                    result = 1;
                }

                if (n1.doubleValue() == n2.doubleValue()) {
                    result = 0;
                }

            } else {
                if (n1.doubleValue() < n2.doubleValue()) {
                    result = -1;
                }

                if (n1.doubleValue() > n2.doubleValue()) {
                    result = 1;
                }

                if (n1.doubleValue() == n2.doubleValue()) {
                    result = 0;
                }
            }
        }

        if (val1 instanceof Float) {
            if (!isASC) {
                if (n1.floatValue() > n2.floatValue()) {
                    result = -1;
                }

                if (n1.floatValue() < n2.floatValue()) {
                    result = 1;
                }

                if (n1.floatValue() == n2.floatValue()) {
                    result = 0;
                }

            } else {
                if (n1.floatValue() < n2.floatValue()) {
                    result = -1;
                }

                if (n1.floatValue() > n2.floatValue()) {
                    result = 1;
                }

                if (n1.floatValue() == n2.floatValue()) {
                    result = 0;
                }
            }
        }
        return result;
    }
}
